package com.hjm.common.utils;


import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 加密工具类
 * md5加密出来的长度是32位
 * sha加密出来的长度是40位
 * 
 */
public class Encrypt {

	/**
	 * 加密
	 * 
	 * @param inputText
	 * @return
	 */
	public static String e(String inputText) {
		return md5(inputText);
	}

	/**
	 * 二次加密，应该破解不了了吧？
	 * 
	 * @param inputText
	 * @return
	 */
	public static String md5AndSha(String inputText) {
		return sha(md5(inputText));
	}

	/**
	 * md5加密
	 * 
	 * @param inputText
	 * @return
	 */
	public static String md5(String inputText) {
		return encrypt(inputText, "md5");
	}

	/**
	 * sha加密
	 * 
	 * @param inputText
	 * @return
	 */
	public static String sha(String inputText) {
		return encrypt(inputText, "sha-1");
	}

	/**
	 * md5或者sha-1加密
	 * 
	 * @param inputText
	 *            要加密的内容
	 * @param algorithmName
	 *            加密算法名称：md5或者sha-1，不区分大小写
	 * @return
	 */
	private static String encrypt(String inputText, String algorithmName) {
		if (inputText == null || "".equals(inputText.trim())) {
			throw new IllegalArgumentException("请输入要加密的内容");
		}
		if (algorithmName == null || "".equals(algorithmName.trim())) {
			algorithmName = "md5";
		}
		String encryptText = null;
		try {
			MessageDigest m = MessageDigest.getInstance(algorithmName);
			m.update(inputText.getBytes("UTF8"));
			byte s[] = m.digest();
			// m.digest(inputText.getBytes("UTF8"));
			return hex(s);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return encryptText;
	}

	/**
	 * 返回十六进制字符串
	 * 
	 * @param arr
	 * @return
	 */
	private static String hex(byte[] arr) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < arr.length; ++i) {
			sb.append(Integer.toHexString((arr[i] & 0xFF) | 0x100).substring(1, 3));
		}
		return sb.toString();
	}

	public static String getEncrypt(String key,String uuid,String reqType, String busiChnl,String time){
		StringBuffer checkCode = new StringBuffer();
		checkCode.append(key);
		checkCode.append(uuid);
		checkCode.append(reqType);
		checkCode.append(busiChnl);
		checkCode.append(time);
		return md5(checkCode.toString());
	}

	/**
	 * 钱宝保单接口
	 * @param params
	 * @return
	 */
	/*public static Map<String,Object> getInsurancePolicyInfo(Map<String,String> params){
		String checkCode = getEncrypt(AutoLoadData.insurancePolicy_key,params.get("uuid"),params.get("reqType"),AutoLoadData.insurancePolicy_busiChnl,params.get("tAcctTm"));
		String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
				"<Packet type=\"REQUEST\" version=\"1.0\">" +
				"    <Head>" +
				"        <UUID>"+params.get("uuid")+"</UUID>" +
				"        <CRequestType>"+params.get("reqType")+"</CRequestType>" +
				"        <CBusiChnl>"+AutoLoadData.insurancePolicy_busiChnl+"</CBusiChnl>" +
				"        <TAcctTm>"+params.get("tAcctTm")+"</TAcctTm>" +
				"        <CheckCode>"+checkCode+"</CheckCode>" +
				"    </Head>" +
				"    <Body>" +
				"        <orderNoList>" +
				"            <COrderNo>"+params.get("plyno")+"</COrderNo>" +
				"        </orderNoList>" +
				"    </Body>" +
				"</Packet>";
		JSONObject json = HttpClientUtils.sendHttpPostXml(AutoLoadData.insurancePolicy_url,xml);
		if(json!=null && json.containsKey("responseCode") && json.getInteger("responseCode") == 200){
			String body = XmlUtil.doXMLParse(json.getString("responseVal")).get("Body").toString();
			Map map = XmlUtil.doXMLParse(body);
			String ply = XmlUtil.doXMLParse(body).get("Ply").toString();
			return XmlUtil.doXMLParse("<xml>"+ply+"</xml>");
		}
		return null;
	}*/
}
